//
//  StartupViewController.m
//  Finder
//
//  Created by cdm on 11-4-3.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StartupViewController.h"
#import "RootViewController.h"

@interface StartupViewController ()

@property (nonatomic, retain) NSMutableArray *startupImages;
@property (nonatomic, assign) int playIndex;

- (void)playStartupImages;
- (void)startImageAppear;
- (void)startImageDisappear;

@end


@implementation StartupViewController

@synthesize startupImageView;
@synthesize startupImages;
@synthesize playIndex;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [self.startupImageView release];
    [self.startupImages release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 }
 */

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    
    self.startupImages = [[NSMutableArray alloc] init];
    
    NSMutableArray *lx = [[NSMutableArray alloc] init];
    [lx addObject:[UIImage imageNamed:@"lx_1.png"]];
    [lx addObject:[UIImage imageNamed:@"lx_2.png"]];
    [lx addObject:[UIImage imageNamed:@"lx_3.png"]];
    [lx addObject:[UIImage imageNamed:@"lx_4.png"]];
    [lx addObject:[UIImage imageNamed:@"lx_5.png"]];
    [self.startupImages addObject:lx];
    [lx release];
    
    NSMutableArray *app = [[NSMutableArray alloc] init];
    [app addObject:[UIImage imageNamed:@"app.png"]];
    [self.startupImages addObject:app];
    [app release];
    
    self.playIndex = 0;
    
    [self playStartupImages];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    self.startupImageView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)playStartupImages
{
    [self startImageAppear];
}

- (void)startImageAppear
{
    if (self.playIndex == [self.startupImages count]) {
        
        self.navigationController.navigationBarHidden = NO;
        
        RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootView" bundle:nil];
        [self.navigationController pushViewController:rootViewController animated:NO];
        [rootViewController release];
        
        return;
    }
    
    self.startupImageView.alpha = 0.0;
    
    NSMutableArray *images = [self.startupImages objectAtIndex:self.playIndex];
    self.startupImageView.animationImages = images;
    self.startupImageView.animationDuration = 0.5;
    [self.startupImageView startAnimating];
    
    [UIView beginAnimations:@"startImageAppearAnim" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startImageDisappear)];
    
    self.startupImageView.alpha = 1.0;
    
    [UIView commitAnimations];
}

- (void)startImageDisappear
{
    [UIView beginAnimations:@"startImageDisappearAnim" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startImageAppear)];
    
    self.startupImageView.alpha = 0.0;
    
    [UIView commitAnimations];
    
    self.playIndex++;
}

@end;